package ycl.security.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import ycl.common.constants.BaseConstant;
import ycl.common.utils.JSONObjectConvertUtil;
import ycl.system.entity.SysUser;

import java.time.LocalDateTime;
import java.util.Collection;

/**
 * @author YCL
 * @date 2022/9/21 0021 12:54
 */
@Getter
@Setter
public class SecurityUser extends User {

	@Schema(description = "id")
	private Long id;

	@Schema(description = "用户头像")
	private String avatar;


	@Schema(description = "登录信息")
	private String token;

	@Schema(description = "用户类型，0超管，1普通用户")
	private Integer type;


	@Schema(description = "手机号")
	private String mobile;

	@Schema(description = "状态，0正常，1禁用")
	private Integer status;

	@Schema(description = "注册时间")
	@JsonFormat(pattern = BaseConstant.DATE_TIME_PATTERN, timezone = BaseConstant.DATE_TIME_GMT8)
	private LocalDateTime registerTime;

	@Schema(description = "登录地址")
	private String loginIp;

	@Schema(description = "登录时间")
	@JsonFormat(pattern = BaseConstant.DATE_TIME_PATTERN, timezone = BaseConstant.DATE_TIME_GMT8)
	private LocalDateTime loginTime;

	@Schema(description = "上次登录时间")
	@JsonFormat(pattern = BaseConstant.DATE_TIME_PATTERN, timezone = BaseConstant.DATE_TIME_GMT8)
	private LocalDateTime lastLoginTime;

	@Schema(description = "上次登录地址")
	private String lastLoginIp;

	@Schema(description = "退出时间")
	@JsonFormat(pattern = BaseConstant.DATE_TIME_PATTERN, timezone = BaseConstant.DATE_TIME_GMT8)
	private LocalDateTime logoutTime;


	/**
	 * ====================================
	 * <p>
	 * 以上是 SysUser 全部字段
	 * <p>
	 * ------------------------------------
	 * <p>
	 * 以下是自定义字段
	 * <p>
	 * ====================================
	 */

	@Schema(description = "是否是管理员")
	private Boolean isAdmin = false;

	@Schema(description = "是否已绑定手机号")
	private Boolean isBindMobile = false;

	@Schema(description = "是否已完成认证")
	private Boolean isAuth = false;


	public SecurityUser(SysUser user, Collection<? extends GrantedAuthority> authorities) {
		super(user.getUsername(), user.getPassword(), authorities);
		JSONObjectConvertUtil.convert(user, this);
	}
}
